package cn.ihuizi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.generic.I2F;

import cn.ihuizi.entity.Role;
import cn.ihuizi.service.RoleService;
import cn.ihuizi.service.impl.RoleServiceImpl;

/**
 * Servlet implementation class InsertRoleServlet
 */
@WebServlet("/InsertRoleServlet")
public class InsertRoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		RoleService roleService = new RoleServiceImpl();
		String rolename = request.getParameter("rolename");
		rolename=new String(rolename.getBytes("ISO-8859-1"),"utf-8");
		String power = request.getParameter("checkpower");
		System.out.println(power);
		boolean b = roleService.insertRoleName(new Role(rolename));
		boolean b1 = roleService.insertPoweridByRoleid(new Role(rolename,power));
		if(b&b1) {
			response.getWriter().write("保存成功");
		}else {
			response.getWriter().write("保存失败");
		}
		/*System.out.println(power);
		List<String> list = new ArrayList<>();
		list.add(power);
		for(int i=0; i<list.size();i++) {
			System.out.println(list.get(1));*/
			/*boolean b1 = roleService.insertPoweridByRoleid(new Role(rolename,list.get(i)));*/
		}
	/*	if(b) {*/
			/*for(int i=0; i<list.size();i++) {
				System.out.println(list.get(1));
				boolean b1 = roleService.insertPoweridByRoleid(new Role(rolename,list.get(i)));
				if(b1) {
					response.getWriter().write("保存成功");
				}else {
					response.getWriter().write("保存失败");
				}
			}*/
		/*}else {
			response.getWriter().write("保存失败");
		}*/
		

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
